/*---------------------------------------------------------------------------------

	by GugloPWN

---------------------------------------------------------------------------------*/

#include "block.h"



void block::readblock(char path[64])
{
	char mapfile[128] = "nitrofiles:/";
	char modelfile[128] = "";
	char textfile[128] = "";
	strcat(mapfile,path);
	strcat(mapfile,".vmap");
	strcat(modelfile,path);
	strcat(modelfile,".md2");
	strcat(textfile,path);
	strcat(textfile,".bmp");
	
	FILE* file = fopen(mapfile,"rb");
	
	if(file == NULL) {printf("\n%s block not found",mapfile); return;}
	printf("\n%s block found",mapfile);
	int temp;
	int size = sizeof(temp);

	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			fread(&temp,size,1,file);
			base[i][j] = temp;
			fread(&temp,size,1,file);
			plat[i][j] = temp;
		}
	}
	rail rtemp;
	size = sizeof(rtemp);
	for(i=0;i<16;i++)
	{
		fread(&rtemp,size,1,file);
		rails[i] = rtemp;
	}
	
	fclose(file);
	
	
	printf("Loading Block Texture\n");
	glGenTextures(1, Texture);
	LoadModelTexture(textfile,Texture,TexturePal,&textsize) ;
	printf("Loading Block Model\n");
	LoadMD2Model(modelfile,textsize,1,Model,ModelEnable);
	printf("Done.\n");
	
	
	return;
}

void block::writeblock(char path[64])
{
	char mapfile[128] = "";
	strcat(mapfile,path);
	strcat(mapfile,".vmap");
	
	FILE* file = fopen(mapfile,"wb");
	
	int temp;
	int size = sizeof(temp);

	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			temp = base[i][j];
			fwrite(&temp,size,1,file);
			temp = plat[i][j];
			fwrite(&temp,size,1,file);
		}
	}
	
	rail rtemp;
	size = sizeof(rtemp);
	for(i=0;i<16;i++)
	{
		rtemp = rails[i];
		fwrite(&rtemp,size,1,file);
	}
	
	fclose(file);
	return;
}

void block::getdefaultblock()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			base[i][j] = 0;
			plat[i][j] = 0;
		}
	}
	base[19][19] = 8;
	base[11][19] = 8;
	base[19][11] = 8;
	base[9][0] = 8;
	base[0][9] = 8;
	
	for(i=0;i<16;i++)
	{
		rails[i].a.x = 21;
		rails[i].a.y = 21;
		rails[i].a.z = 21;
		rails[i].b.x = 21;
		rails[i].b.y = 21;
		rails[i].b.z = 21;
	}
}

void block::getnullblock()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			base[i][j] = 100;
			plat[i][j] = 100;
		}
	}
	
	for(i=0;i<16;i++)
	{
		rails[i].a.x = 21;
		rails[i].a.y = 21;
		rails[i].a.z = 21;
		rails[i].b.x = 21;
		rails[i].b.y = 21;
		rails[i].b.z = 21;
	}
}

void block::gettestblock()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			base[i][j] = 20;
			plat[i][j] = 0;
		}
	}
	
	for(i=0;i<16;i++)
	{
		base[6][i] = 100;
	}
	
	for(i=0;i<15;i++)
	{
		for(j=16;j<20;j++)
		{
			plat[i][j] = 72;
		}
	}
	
	for(i=0;i<16;i++)
	{
		rails[i].a.x = 21;
		rails[i].a.y = 21;
		rails[i].a.z = 21;
		rails[i].b.x = 21;
		rails[i].b.y = 21;
		rails[i].b.z = 21;
	}
	rails[0].a.x = 15;
	rails[0].a.y = 16;
	rails[0].a.z = 72;
	rails[0].b.x = 20;
	rails[0].b.y = 0;
	rails[0].b.z = 44;
}

void block::loadblockmodel()
{
	/*
	printf("Loading Block Texture\n");
	glGenTextures(10, &Texture);
	LoadModelTexture("warehouse.bmp",Texture,TexturePal,&textsize) ;
	printf("Loading Block Model\n");
	LoadMD2Model("warehouse.md2",textsize,1,Model,ModelEnable);
	printf("Done.\n");
	*/
}

void block::drawblock()
{
	if(ModelEnable == true)
	{

		glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
		glPushMatrix();
	
	glRotatef(270,0.0f,0.0f,1.0f);
	glScalef(12.8,12.8,12.8);
	
		glBindTexture (GL_TEXTURE_2D, Texture[0]);
		glColorTable(GL_RGB256, TexturePal[0]);		
		RenderMD2Model(0,Model,ModelEnable);
	
		glPopMatrix(1);
	}
}

void block::drawblockcolmap()
{
	if(ModelEnable == false)
	{
		glPolyFmt(POLY_ALPHA(0) | POLY_CULL_NONE);
		
		//glLoadIdentity(); 
		//glRotatef(-30,1.0f,0,0);
		//glTranslatef(-1.40,0,-1.6f);
		//glPushMatrix();
		//glTranslatef(-0.05,-0.05,0.0f);
	
	
	
	
		int i,j;
		glBegin(GL_QUADS);
		for(i=0;i<20;i++)
		{
			for(j=0;j<20;j++)
			{
				//Draw the Base
				glVertex3f( ((float)i)/10, ((float)j)/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i+1))/10, ((float)j)/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i+1))/10, ((float)(j+1))/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i))/10, ((float)(j+1))/10, ((float)base[i][j])/40);
			
				//Draw the platform
				if(plat[i][j] != 0)
				{
					glColor3f(0.0f,0.0f,0.0f);
					glVertex3f( ((float)i)/10, ((float)j)/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i+1))/10, ((float)j)/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i+1))/10, ((float)(j+1))/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i))/10, ((float)(j+1))/10, ((float)plat[i][j])/40);
					glColor3f(1.0f,1.0f,1.0f);
				}
			}
		}
		glColor3f(0.0f,1.0f,0.0f);
		for(i=0;i<16;i++)
		{
			if(rails[i].a.x != 21)
			{
				glVertex3f( ((float)rails[i].a.x)/10,((float)rails[i].a.y)/10,((float)rails[i].a.z)/40);
				glVertex3f( ((float)rails[i].a.x)/10,((float)rails[i].a.y)/10,((float)rails[i].a.z-0.05)/40);
				glVertex3f( ((float)rails[i].b.x)/10,((float)rails[i].b.y)/10,((float)rails[i].b.z-0.05)/40);
				glVertex3f( ((float)rails[i].b.x)/10,((float)rails[i].b.y)/10,((float)rails[i].b.z)/40);
			}
		}
		
		glColor3f(1.0f,0.0f,1.0f);
		glVertex3f(0.0,0.0, -0.025);
		glVertex3f(2.0,0.0, -0.025);
		glVertex3f(2.0,2.0, -0.025);
		glVertex3f(0.0,2.0, -0.025);
	
	
	
		glEnd();
		//glPopMatrix(1);
	
		glPolyFmt(POLY_ALPHA(31) | POLY_CULL_FRONT);
		glColor3f(1.0f,1.0f,1.0f);
	}
	return;
}

